package ar.com.ctrln.persistence.model.sistema.cuentas;

import java.util.Collection;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import ar.com.ctrln.persistence.model.contacto.Contacto;
import ar.com.ctrln.persistence.model.evento.EventoPersonal;

@Entity
@Table(name = "USUARIO")
public class Usuario implements UserDetails {

	/**
	 * 
	 */

	private static final long serialVersionUID = -3738745647973350703L;

	@Id
	@GeneratedValue
	@Column(name = "id_usuario")
	private long id;

	@Column(name = "nombre", nullable = false)
	private String nombre;

	@Column(name = "password", nullable = false)
	private String password;

	@Column(name = "expirada", nullable = false)
	private boolean expirada;

	@Column(name = "bloqueada", nullable = false)
	private boolean bloqueada;

	@Column(name = "credencial_expirada", nullable = false)
	private boolean credencialExpirada;

	@Column(name = "activada", nullable = false)
	private boolean activada;

	@ManyToMany(fetch = FetchType.EAGER)
	@JoinTable(name = "USUARIO_AUTORIZACION", joinColumns = { @JoinColumn(name = "id_usuario") }, inverseJoinColumns = { @JoinColumn(name = "id_autorizacion") })
	private Set<Autorizacion> autorizaciones;

	@OneToMany(mappedBy = "usuario", fetch = FetchType.LAZY)
	private Collection<EventoPersonal> eventos;
	
	@OneToOne(mappedBy = "usuario", fetch = FetchType.EAGER)
	private Contacto contacto;

	public Usuario() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public Set<? extends GrantedAuthority> getAuthorities() {
		// TODO Auto-generated method stub
		return autorizaciones;
	}

	@Override
	public String getPassword() {
		// TODO Auto-generated method stub
		return this.password;
	}

	@Override
	public String getUsername() {
		// TODO Auto-generated method stub
		return this.nombre;
	}

	public Contacto getContacto() {
		return contacto;
	}

	public void setContacto(Contacto contacto) {
		this.contacto = contacto;
	}

	@Override
	public boolean isAccountNonExpired() {
		// TODO Auto-generated method stub
		return this.expirada;
	}

	@Override
	public boolean isAccountNonLocked() {
		// TODO Auto-generated method stub
		return this.bloqueada;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		// TODO Auto-generated method stub
		return this.credencialExpirada;
	}

	@Override
	public boolean isEnabled() {
		// TODO Auto-generated method stub
		return this.activada;
	}

	public String getNombre() {
		return nombre;
	}

	public void setNombre(String nombre) {
		this.nombre = nombre;
	}

	public long getId() {
		return id;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setExpirada(boolean expirada) {
		this.expirada = expirada;
	}

	public void setBloqueada(boolean bloqueada) {
		this.bloqueada = bloqueada;
	}

	public void setCredencialExpirada(boolean credencialExpirada) {
		this.credencialExpirada = credencialExpirada;
	}

	public void setActivada(boolean activada) {
		this.activada = activada;
	}

	public void setAuthorities(Set<Autorizacion> roles) {
		this.autorizaciones = roles;
	}

	public void setEventos(Collection<EventoPersonal> eventos) {
		this.eventos = eventos;
	}

	public Collection<EventoPersonal> getEventos() {
		return eventos;
	}
	
	public Collection<Autorizacion> getAutorizaciones() {
		return autorizaciones;
	}
}
